package com.example.controller;


import com.example.common.exception.BaseException;
import com.example.common.result.Result;
import com.example.entity.Travel;
import com.example.entity.dto.TravelSearch;
import com.example.entity.vo.TravelVO;
import com.example.service.TravelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("/travel")
@Slf4j
@Api(tags = "差旅相关接口")
public class TravelController {

    @Autowired
    private TravelService travelService;

    /**
     * 获取当前项目用车
     * @param projectId
     * @return
     */
    @GetMapping("/getlist")
    @ApiOperation("获取当前项目用车")
    private Result<List<TravelVO>> findByProjectId(@RequestParam("projectId") int projectId,@RequestParam String time){
        log.info("获取当前项目用车:{},{}",projectId,time);
        List<TravelVO> travelList = travelService.findByProjectId(projectId, time);
        return Result.success(travelList);
    }

    /**
     * 新增当前项目用车
     * @param travel
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("新增当前项目用车")
    private Result addTravel(@RequestBody Travel travel){
        log.info("新增当前项目用车：{}",travel);
        travelService.addTravel(travel);
        return Result.success();
    }

    /**
     * 更新当前用车
     * @param travel
     * @return
     */
    @PostMapping("/updata")
    @ApiOperation("更新当前用车")
    private Result updataTravelById(@RequestBody Travel travel){
        log.info("更新当前项目用车id:{}",travel);
        travelService.updataTravelById(travel);
        return Result.success();
    }

    /**
     * 获取当前用车
     * @param id
     * @return
     */
    @GetMapping("/get")
    @ApiOperation("获取当前用车")
    private Result<TravelVO> findTravelById(@RequestParam int id){
        log.info("获取当前用车:{}",id);
        TravelVO travel = travelService.findById(id);
        return Result.success(travel);
    }

    /**
     * 删除用车
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation("删除用车")
    private Result deleteTravel(@RequestParam int id){
        log.info("删除用车:{}",id);
        travelService.deleteTravel(id);
        return Result.success();
    }

    /**
     * 用车查询
     * @param travelSearch
     * @return
     */
    @PostMapping("/search")
    @ApiOperation("用车查询")
    private Result<List<TravelVO>> search(@RequestBody TravelSearch travelSearch){
        log.info("用车查询:{}",travelSearch);
        List<TravelVO> list = travelService.search(travelSearch);
        return Result.success(list);
    }
}
